package com.bonc.vectorspace.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.NlpAnalysis;

/**
 * @author donggui@bonc.com.cn
 * @version 2016 2016年6月27日 上午11:31:26
 */
public class EventDocument implements Comparable<EventDocument>{
	
	private String docId;
	
	private String content;
	
	private String tag;
	
	private Map<String, String> stopwords;

	private HashMap<String, Integer> termFrequency;
	
	//4
	public EventDocument(String docId, String content,String tag, Map<String, String> stopwordsChi) {

		this.docId = docId;
		this.content = content.trim();
		termFrequency = new HashMap<String, Integer>();
		this.stopwords = stopwordsChi;
		if(tag !=null){
			this.setTag(tag);
		}
		
		readContentAndPreProcess();
		
	}

	private void readContentAndPreProcess(){
		if(content!=null){
			List<Term> terms = NlpAnalysis.parse(this.content.trim());
			String regex = "(?!^[\\d]+$)^[a-zA-Z0-9\u4E00-\u9FA5]+$";

			String trimWord = "";
			for (Term term : terms) {
				trimWord = term.getName().trim();
				if (trimWord!=null && !trimWord.isEmpty() && trimWord.length() > 1 
						&& !this.stopwords.containsKey(trimWord)
						&& trimWord.matches(regex)) {

					if (termFrequency.containsKey(trimWord)) {
						int oldCount = termFrequency.get(trimWord);
						termFrequency.put(trimWord, ++oldCount);
//						System.out.println("putting term=="+trimWord);
					} else {
						termFrequency.put(trimWord, 1);
					}
				}
			}			
		}
	}
	
	public Set<String> getTermList() {
		return termFrequency.keySet();
	}
	
	public double getTermFrequency(String word) {
		if (termFrequency.containsKey(word)) {
			return termFrequency.get(word);
		} else {
			return 0;
		}
	}

	public HashMap<String, Integer> getTermFrequency() {
		return termFrequency;
	}

	public void setTermFrequency(HashMap<String, Integer> termFrequency) {
		this.termFrequency = termFrequency;
	}

	public String getDocId() {
		return docId;
	}

	public void setDocId(String docId) {
		this.docId = docId;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Map<String, String> getStopwords() {
		return stopwords;
	}

	public void setStopwords(Map<String, String> stopwords) {
		this.stopwords = stopwords;
	}


	@Override
	public String toString() {
		return "EventDocument [docId=" + docId + ", content=" + content + "]";
	}
	

	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(EventDocument other) {
		return docId.compareTo(other.getDocId());
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}
}
